//
//  AddClassTypeViewController.m
//  CoachsBook
//
//  Created by huiyong li on 2022/8/19.
//

#import "AddClassTypeViewController.h"
#import "OneTabButView.h"
#import "CoachsBookInputModel.h"
#import "CoachsBookTableHeaderCell.h"
#import "CoachsBookTextFieldCell.h"
#import "CoachsBookTextViewCell.h"
#import "LongPickerView.h"
#import "LongPickerViewModel.h"
#import "LongPickerTagView.h"


@interface AddClassTypeViewController ()<UITableViewDelegate,UITableViewDataSource,OneTabButViewDelegate,UITextFieldDelegate,LongPickerViewDelegate,LongPickerTagViewDelegate>
@property (nonatomic,strong) LongPickerView *myPickerView;
@property (nonatomic,strong) LongPickerTagView *myPickerTagView;
@end

@implementation AddClassTypeViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    self.navgationheadv.titlelabel.text = self.VCType==1?@"添加班型":@"修改班型";
    
    OneTabButView *tabbutview = [[OneTabButView alloc]initWithFrame:CGRectMake(0, APPheight-SafeAreaBottomHeight-65, APPwidth, 65)];
    tabbutview.delegate = self;
    [tabbutview.tabbut setTitle:@"保存" forState:UIControlStateNormal];
    [self.view addSubview:tabbutview];
    
    [self getData];
}

- (void)getData
{
    NSArray *titlearr = @[];
    NSArray *promptarr = @[];
    NSArray *celltypearr = @[];
    if (self.VCType == 1) {
        titlearr = @[@"驾照类型",@"班级类型",@"班型说明",@"其他信息",@"练车车型",@"课程费用",@"收费模式",@"拿本时长",@"接送方式",@"学车时间",@"几人一车",@"约车方式",@"特殊服务",@"适合人群",@"详细描述"];
        promptarr = @[@"请选择",@"请选择",@"补充备注",@"分段cell",@"请选择",@"课程费用不可低于500元",@"请选择",@"请选择",@"请选择",@"请选择",@"请填写每车人数",@"请选择",@"请选择",@"请选择",@"描述详情"];
        celltypearr = @[@"1",@"1",@"1",@"10",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"1",@"4"];
        for (NSInteger i=0; i<titlearr.count; i++) {
            CoachsBookInputModel *model = [[CoachsBookInputModel alloc]init];
            model.title = titlearr[i];
            model.prompt = promptarr[i];
            model.cellType = [celltypearr[i] integerValue];
            model.showArrow = YES;
            if (i==2||i==3||i==5||i==10||i==14) {
                model.showArrow = NO;
            }
            [self.dataArray addObject:model];
        }
    }
    else{
        
    }

}

- (void)setupUI {
    [super setupUI];
    self.tableView.frame = CGRectMake(0, SafeAreaTopHight, APPwidth, APPheight-SafeAreaTopHight-SafeAreaBottomHeight-65);
    UIView *HeaderV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, APPwidth, 45)];
    HeaderV.backgroundColor = [UIColor whiteColor];
    
    UIView *myDataLineV = [[UIView alloc]initWithFrame:CGRectMake(15, 15, 3, 15)];
    myDataLineV.backgroundColor = rgba(22, 120, 255, 1);
    myDataLineV.layer.cornerRadius = 2;
    [HeaderV addSubview:myDataLineV];
    UILabel *myDataLab = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMaxX(myDataLineV.frame)+5, 15, 150, 15)];
    myDataLab.text = @"详细信息";
    myDataLab.textColor = [UIColor blackColor];
    myDataLab.textAlignment = 0;
    myDataLab.font = kPingFangSemibold(16);
    [HeaderV addSubview:myDataLab];
    
    self.tableView.tableHeaderView = HeaderV;
    [self setMyPickerView];
    [self setMyPickerTagView];
}

#pragma mark - 设置自定义选择器
-(void)setMyPickerView
{
    self.myPickerView = [[LongPickerView alloc]initWithFrame:CGRectMake(0, 0, APPwidth, APPheight) dataArr:@[]];
    [[UIApplication sharedApplication].keyWindow addSubview:self.myPickerView];
    self.myPickerView.delegate = self;
    self.myPickerView.hidden = YES;
}

#pragma mark - 设置tag选择器
-(void)setMyPickerTagView
{
    self.myPickerTagView = [[LongPickerTagView alloc]initWithFrame:CGRectMake(0, 0, APPwidth, APPheight) dataArr:@[]];
    self.myPickerTagView.maxnumber = 1;
    self.myPickerTagView.widthNumber = 3;
    [[UIApplication sharedApplication].keyWindow addSubview:self.myPickerTagView];
    self.myPickerTagView.delegate = self;
    self.myPickerTagView.hidden = YES;
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == self.dataArray.count-1) {
        return 260;
    }
    else if (indexPath.row == 3)
    {
        return 55;
    }
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CoachsBookInputModel *model = self.dataArray[indexPath.row];
    if (model.cellType == 1) {
        CoachsBookTextFieldCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CoachsBookTextFieldCell"];
        if (!cell) {
            cell=[[CoachsBookTextFieldCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CoachsBookTextFieldCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell cellFillWithModel:model];
        cell.contentField.tag = indexPath.row;
        cell.contentField.delegate = self;
        return cell;
    }
    else if (model.cellType == 10)
    {
        CoachsBookTableHeaderCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CoachsBookTableHeaderCell"];
        if (!cell) {
            cell=[[CoachsBookTableHeaderCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CoachsBookTableHeaderCell"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell cellFillWithModel:model];
        return cell;
        
    }
    CoachsBookTextViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"CoachsBookTextViewCell"];
    if (!cell) {
        cell=[[CoachsBookTextViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"CoachsBookTextViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell cellFillWithModel:model];
    cell.CertificationTextViewBlock = ^(NSString * _Nonnull textViewText) {
        NSLog(@"99999=%@",textViewText);
        model.content = textViewText;
        [self.tableView reloadData];
    };
    return cell;
    
}



#pragma mark - UITextFieldDelegate
- (void)textFieldDidBeginEditing:( UITextField*)textField
{
    CoachsBookInputModel *model = self.dataArray[textField.tag];
    if (model.showArrow) {
        UIView *dummyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        textField.inputView = dummyView; // Hide keyboard,but show blinking cursor
        NSMutableArray *PickerViewModelArr = [[NSMutableArray alloc]init];
        [PickerViewModelArr removeAllObjects];
        self.myPickerView.tag = textField.tag;
        self.myPickerTagView.tag = textField.tag;
        if (textField.tag == 0) {
            self.myPickerTagView.titleLabel.text = @"请选择驾照类型";
            self.myPickerTagView.dataArr = @[@"  C1  ",@"  C2  ",@"  C3  ",@"  C4  ",@"  C5  ",@"  C6  ",@"  A1  ",@"  A2  ",@"  A3  ",@"  B1  ",@"  B2  ",@"  D  ",@"  E  ",@"  F  "];
            self.myPickerTagView.hidden = NO;
        }
        else if (textField.tag == 1) {
            self.myPickerTagView.titleLabel.text = @"请选择班级类型";
            self.myPickerTagView.dataArr = @[@"普通班",@"周末班",@"晚间班",@"VIP班",@"外地班",@"速成班",@"平日班",@"老年班",@"女士班",@"特价班",@"假期班",@"全周班",@"七天火箭班",@"私人定制班",@"其它"];
            self.myPickerTagView.hidden = NO;
        }
        else if (textField.tag == 6)
        {
            NSArray *titlearr = @[@"传统模式",@"支持分期",@"计时付费",@"先学后付"];
            NSArray *promptarr = @[@"报名即全交费用",@"可以分期付款",@"根据上课学时缴费",@"先培训后付钱"];
            for (NSInteger i = 0; i < titlearr.count; i++) {
                LongPickerViewModel *model =  [[LongPickerViewModel alloc]init];
                model.title = titlearr[i];
                model.prompt = promptarr[i];
                [PickerViewModelArr addObject:model];
            }
            self.myPickerView.pickerViewType = LongDoubleRow;
            self.myPickerView.dataArr = PickerViewModelArr;
            self.myPickerView.hidden = NO;
        }
        else if (textField.tag == 7)
        {
            NSArray *titlearr = @[@"45天以内",@"60天以内",@"90天以内",@"180天以内",@"半年以上"];
            for (NSString *title in titlearr) {
                LongPickerViewModel *model =  [[LongPickerViewModel alloc]init];
                model.title = title;
                [PickerViewModelArr addObject:model];
            }
            self.myPickerView.pickerViewType = LongSingleLine;
            self.myPickerView.dataArr = PickerViewModelArr;
            self.myPickerView.hidden = NO;
        }
        else if (textField.tag == 8)
        {
            NSArray *titlearr = @[@"教练接送",@"班车接送",@"自行前往"];
            for (NSString *title in titlearr) {
                LongPickerViewModel *model =  [[LongPickerViewModel alloc]init];
                model.title = title;
                [PickerViewModelArr addObject:model];
            }
            self.myPickerView.pickerViewType = LongSingleLine;
            self.myPickerView.dataArr = PickerViewModelArr;
            self.myPickerView.hidden = NO;
        }
        else if (textField.tag == 9)
        {
            NSArray *titlearr = @[@"周一到周五训练",@"周一到周日训练",@"周末训练",@"夜间训练"];
            for (NSString *title in titlearr) {
                LongPickerViewModel *model =  [[LongPickerViewModel alloc]init];
                model.title = title;
                [PickerViewModelArr addObject:model];
            }
            self.myPickerView.pickerViewType = LongSingleLine;
            self.myPickerView.dataArr = PickerViewModelArr;
            self.myPickerView.hidden = NO;
        }
        else if (textField.tag == 11)
        {
            NSArray *titlearr = @[@"自助协商",@"预留车辆",@"免预约",@"自主约车",@"统一安排",@"电话预约"];
            for (NSString *title in titlearr) {
                LongPickerViewModel *model =  [[LongPickerViewModel alloc]init];
                model.title = title;
                [PickerViewModelArr addObject:model];
            }
            self.myPickerView.pickerViewType = LongSingleLine;
            self.myPickerView.dataArr = PickerViewModelArr;
            self.myPickerView.hidden = NO;
        }
        else if (textField.tag == 12)
        {
            NSArray *titlearr = @[@"无",@"免费补考",@"赠送配练",@"专享VIP服务",@"一费到底",@"私人定制"];
            for (NSString *title in titlearr) {
                LongPickerViewModel *model =  [[LongPickerViewModel alloc]init];
                model.title = title;
                [PickerViewModelArr addObject:model];
            }
            self.myPickerView.pickerViewType = LongSingleLine;
            self.myPickerView.dataArr = PickerViewModelArr;
            self.myPickerView.hidden = NO;
        }
        else if (textField.tag == 13)
        {
            NSArray *titlearr = @[@"通用",@"学车时间不确定",@"学车周期长",@"快速拿本",@"高端人士",@"上班族",@"学生群体",@"学车时间无工作安排",@"女士"];
            for (NSString *title in titlearr) {
                LongPickerViewModel *model =  [[LongPickerViewModel alloc]init];
                model.title = title;
                [PickerViewModelArr addObject:model];
            }
            self.myPickerView.pickerViewType = LongSingleLine;
            self.myPickerView.dataArr = PickerViewModelArr;
            self.myPickerView.hidden = NO;
        }
    }
    
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    CoachsBookInputModel *model = self.dataArray[textField.tag];
    if (textField.text.length) {
        model.content = textField.text;
    }
}

#pragma mark - CYWKLPickerViewDelegate
-(void)choosPickerView:(LongPickerView *)pickerView WithRowStr:(NSString *)rowStr AndRowNum:(NSInteger)row
{
    CoachsBookInputModel *model = self.dataArray[pickerView.tag];
    model.content = rowStr;
    NSIndexPath *  indexPath = [NSIndexPath indexPathForRow:pickerView.tag inSection:0];
    CoachsBookTextFieldCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.contentField.text = rowStr;
    [self.view endEditing:YES];
    
}

-(void)closePickerView:(LongPickerView *)pickerView
{
    [self.view endEditing:YES];
}

#pragma mark - CYWKLPickerTagViewDelegate
- (void)choosPickerView :(LongTagView *)tagView  WithchoosStrArr:(NSArray *)strArr
{
    CoachsBookInputModel *model = self.dataArray[tagView.tag];
    NSString *srt = [strArr componentsJoinedByString:@","];
    model.content = srt;
    NSIndexPath *  indexPath = [NSIndexPath indexPathForRow:tagView.tag inSection:0];
    CoachsBookTextFieldCell * cell = [self.tableView cellForRowAtIndexPath:indexPath];
    cell.contentField.text = srt;
    [self.view endEditing:YES];
}

- (void)closePickerTagView :(LongTagView *)tagView{
    [self.view endEditing:YES];
}


#pragma mark -OneTabButViewDelegate(保存)
- (void)OneTabButViewBtnSelectedDelegate:(NSInteger)tag
{
    
}
@end
